package com.UdpRecieve.utils;

import com.UdpRecieve.domain.FilePigeonhole;
import com.UdpRecieve.framework.ExceptionBusiness.BusinessIoException;
import com.UdpRecieve.tool.MD5FileUtils;

import java.io.File;
import java.util.List;
import java.util.regex.Matcher;

public class FileUtiles {
    /**
     * 检验文件完整新(md5)
     *
     * @param filePath       文件全路径
     * @param filePigeonhole 文件对象
     * @throws Exception 文件不存在 或 校验失败
     */
    public static void fileCheckMd5(String filePath, FilePigeonhole filePigeonhole) throws Exception {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new BusinessIoException("落地文件:" + file.getPath() + "不存在!");
        } else {
            //检测MD5(文件完整性)
            String ownFileMd5 = MD5FileUtils.getMD5(file);
            if (!filePigeonhole.getMd5CheckNum().equals(ownFileMd5)) {
                throw new BusinessIoException("文件完整性校验失败!");
            }
        }

    }

    /**
     * 兼容路径
     *
     * @param Path
     * @return
     */
    public static String intitPath(String Path) {
        if (Path.contains("\\")) {
            Path = Path.replaceAll("\\\\", Matcher.quoteReplacement(File.separator));
        } else if (Path.contains("/")) {
            Path = Path.replaceAll("/", Matcher.quoteReplacement(File.separator));
        }
        return Path;
    }


    public static List<String> getFileNames(List<String> fileNames, File file) {
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isDirectory()) {
                getFileNames(fileNames, f);
            } else {
                fileNames.add(f.getName());
            }
        }
        return fileNames;
    }


    public static int setInt() {
        int result = 0xffffffff;
        result &= ~(1 << 0);
        return result;
    }

    public static void main(String[] args) {
        System.out.println(setInt());
    }
}
